package com.gxc.config;

import com.gxc.interceptor.LoginInterceptor;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 拦截器，静态资源访问，页面跳转等.
 *
 * @author 宫新程
 * @since 2018/12/3 8:48 PM
 */
@Configuration
public class WebMvcConfigBean implements WebMvcConfigurer {

  @Override
  public void configurePathMatch(PathMatchConfigurer configurer) {
    configurer.setUseSuffixPatternMatch(false);
  }

  /** 静态资源访问. */
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/my/**");
  }

  /** 页面跳转. */
  @Override
  public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/toLogin").setViewName("login");
  }

  /** 文件上传. 由于SpringBoot已经配置了此组件. */
  //  @Bean
  //  public MultipartResolver multipartResolver() {
  //    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
  //    multipartResolver.setDefaultEncoding("UTF-8");
  //    multipartResolver.setMaxUploadSize(1000000);
  //    multipartResolver.setMaxUploadSizePerFile(1000000);
  //    return multipartResolver;
  //  }

//  /** 配置拦截器. */
//  @Override
//  public void addInterceptors(InterceptorRegistry registry) {
//    List<String> pathUri = new ArrayList<>();
//    pathUri.add("/login");
//    pathUri.add("/toLogin");
//    pathUri.add("/bootstrap-3.3.7-dist/**");
//    pathUri.add("/css/**");
//    pathUri.add("/fonts/**");
//    pathUri.add("/js/**");
//    pathUri.add("/images/**");
//    pathUri.add("/vanilla-cream-css/**");
//    registry
//        .addInterceptor(new LoginInterceptor())
//        .addPathPatterns("/**")
//        .excludePathPatterns(pathUri);
//  }
}
